<?php
/**
 * @author     ：lhw
 * @date       ：Created in 2019/8/4 11:03
 */

namespace app\api\controller;

use app\api\model\Pharmacy as PharmacyModel;
use app\api\service\PharmacyService;
use app\api\vo\ResultVO;

class PharmacyController extends BaseController
{
    public function register($name,$address,$telphone,$yyzz_url,$ylqx_url,$ypjy_url,$parent_id,$login,$password,$others_url){
        if(PharmacyService::checkregister($login)){
            $pharmacy = PharmacyModel::create([
                'name' => $name,
                'address' => $address,
                'telphone' => $telphone,
                'yyzz_url' => $yyzz_url,
                'ylqx_url' => $ylqx_url,
                'ypjy_url' => $ypjy_url,
                'parent_id' => $parent_id,
                'login' => $login,
                'password' => $password,
                'others_url' => $others_url
            ]);

            return json(new ResultVO([
                'data' => $pharmacy->id
            ]));
        }
        return json(new ResultVO(['code' => 1]));
    }

    public function login($login,$password){
        $pharmacy = PharmacyModel::where([
            'login' => $login,
            'password' => $password
        ])->find();

        if($pharmacy === null){
            return json(new ResultVO(['code' => 1]));
        }

        if($pharmacy['parent_id'] == 0)
            return json(new ResultVO(['data' => ['self' => $pharmacy]]));

        $parent = PharmacyModel::where(['id' => $pharmacy['parent_id']])->find();

        return json(new ResultVO(['data' => ['self' => $pharmacy,'parent' => $parent]]));
    }

    public function son_list($id){
        $pharmacy = PharmacyModel::find($id);

        if($pharmacy->parent_id !=0){
            return json(new ResultVO(['code' => 1]));
        }

        $son_list = PharmacyModel::where(['parent_id' => $id])->select();

        return json(new ResultVO(['data' => $son_list]));
    }

    public function update($id,$name,$address,$telphone,$yyzz_url,$ylqx_url,$ypjy_url,$login,$password,$others_url){
        $pharmacy = PharmacyModel::update([
            'name' => $name,
            'address' => $address,
            'telphone' => $telphone,
            'yyzz_url' => $yyzz_url,
            'ylqx_url' => $ylqx_url,
            'ypjy_url' => $ypjy_url,
            'login' => $login,
            'password' => $password,
            'others_url' => $others_url
        ],['id' => $id]);

        return json(new ResultVO());
    }

}